package healththink

class User extends SecUser{
  //needs one Email, not many
  String email
	String firstName
	String lastName
	Date birthday
	
    static hasMany = [facilityScheduleEntries: FacilityScheduleEntry, 
      facilityAttributes: Attribute,
      facilityAttributeRatings: FacilityAttributeRating, 
      facilities: FacilityUserLink, 
      facilityGroups: FacilityGroupUserLink, 
      addresses: Address, 
      telephoneNumbers: TelephoneNumber, 
			additionalEmails: Email, 
      forums: Forum,
      posts: Post]
	
    //static mappedBy = [facilities:"user"]
	
   static constraints = {
    email(email:true, unique:true, blank:false)
    firstName(size:1..64, blank: false)
    lastName(size:1..64, blank: false)
    birthday(nullable:true, blank:true)
    }
    
    String toString(){
        firstName + " " + lastName
    }
    
    /* FacilityUserLink */
    List facilities()
    {
        return facilities.collect{it.facility}
    }
    
    List addToFacilities(Facility facility)
    {
        FacilityUserLink.link(this, facility)
        return facilities()
    }
    
    List removeFromFacilities(Facility facility)
    {
        FacilityUserLink.unlink(this, facility)
        return facilities()
    }
    
    /* FacilityGroupUserLink */
    List facilityGroups()
    {
        return facilityGroups.collect{it.facilityGroup}
    }
    
    List addToFacilityGroups(FacilityGroup facilityGroup)
    {
        FacilityGroupUserLink.link(this, facilityGroup)
        return facilityGroups()
    }
    
    List removeFromFacilityGroups(FacilityGroup facilityGroup)
    {
        FacilityGroupUserLink.unlink(this, facilityGroup)
        return facilityGroups()
    }
}

enum UserType{
	Contributor("Contributor"),
	Owner("Owner"),
	Moderator("Moderator")
	
	final String value
	
	UserType(String value)
	{
		this.value=value
	}
}